﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ITAP.ShprossesBuilder.Properties;
using System.Windows.Forms;
using ITAP.ShprossesBuilder.Editor;
using System.Drawing;
using ITAP.ShprossesBuilder.Modelling.Components;
using ITAP.ShprossesBuilder.Modelling.Components.Actions;
using ITAP.ShprossesBuilder.Modelling.Comparers;

namespace ITAP.ShprossesBuilder.Operations
{
    public class OperationAlignRelativeToModelHorizontal : IOperation
    {
        private ModelControl modelControl;
        #region IOperation Members

        public string Name
        {
            get { return "Выравнивание соединителей по горизонтали относительно модели"; }
        }

        public OperationGroup OperationGroup
        {
            get { return OperationGroup.ShprossConnectorOperation; }
        }

        public System.Drawing.Image Thumbnail
        {
            get { return Resources.ModelHorizontal.ToBitmap(); }
        }

        public void Execute(ModelControl modelControl)
        {
            this.modelControl = modelControl;
            ContextMenuStrip menuStrip = new ContextMenuStrip();

            EventHandler handler = new EventHandler(OnItemClick);
            ToolStripItem inner = new ToolStripMenuItem("По внутренним размерам модели", null, handler);
            inner.Tag = AlignRelative.ToInnerModelSize;
            menuStrip.Items.Add(inner);
            ToolStripItem outer = new ToolStripMenuItem("По внешним размерам модели", null, handler);
            outer.Tag = AlignRelative.ToModelSize;
            menuStrip.Items.Add(outer);
            ToolStripItem cell = new ToolStripMenuItem("По размерам ячейки", null, handler);
            cell.Tag = AlignRelative.ToModelCellSize;
            menuStrip.Items.Add(cell);

            ToolStripItem bounds = new ToolStripMenuItem("По крайним коннекторам", null, handler);
            bounds.Tag = AlignRelative.ToBounds;
            menuStrip.Items.Add(bounds);

            menuStrip.Show(Cursor.Position);             
        }

        public bool CanUndo
        {
            get { return true; }
        }

        #endregion

        private void OnItemClick(object sender, EventArgs e)
        {
            ToolStripMenuItem item = sender as ToolStripMenuItem;
            AlignRelative align = (AlignRelative)item.Tag;

            RectangleF boundingBox = new RectangleF();
            bool indent = true;
            switch (align)
            {
                case AlignRelative.ToModelSize:
                    boundingBox = ((ModelCell)modelControl.ModelContainer.ActiveCell).Model.BoundingBox;
                    break;
                case AlignRelative.ToInnerModelSize:
                    Model model = ((ModelCell)modelControl.ModelContainer.ActiveCell).Model;
                    boundingBox = model.BoundingBox;
                    boundingBox = new RectangleF(boundingBox.X + (float)model.SpacerWidth, boundingBox.Y + (float)model.SpacerWidth, boundingBox.Width - 2 * (float)model.SpacerWidth, boundingBox.Height - 2 * (float)model.SpacerWidth);
                    break;
                case AlignRelative.ToModelCellSize:
                    boundingBox = ((ModelCell)modelControl.ModelContainer.ActiveCell).BoundingBox;
                    break;
                case AlignRelative.ToBounds:
                    indent = false;
                    List<IComponent> connectors = new List<IComponent>();
                    IComponent firstSelectedConnector = null;

                    foreach (IComponent selectedComponent in this.modelControl.ModelContainer.SelectedComponentsStack)
                    {
                        if (selectedComponent is ShprossConnector)
                        {
                            connectors.Add(selectedComponent);
                            if(firstSelectedConnector == null)
                                firstSelectedConnector = selectedComponent;
                        }
                    }
                    //connectors.Sort(new ComponentXAxisComparer());
                    boundingBox = new RectangleF((float)connectors[0].Position.X, (float)firstSelectedConnector.Position.Y, 
                        (float)(connectors[connectors.Count - 1].Position.X - connectors[0].Position.X), 0);
                    break;
            }

            foreach (IComponent selectedComponent in modelControl.ModelContainer.SelectedComponentsStack)
                if (selectedComponent is ShprossConnector)
                {
                    IComponentAction alignAction = new ActionAlignComponentsPositionsRelativeToModel(modelControl, new Type[] { typeof(ShprossConnector) }, ComponentAlign.Horizontal, boundingBox, indent);
                    alignAction.Action(selectedComponent);
                    break;
                }
            this.modelControl.UpdateModel();
        }

    }
}
